package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GysptTest;
import com.ruoyi.system.service.IGysptTestService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 测试项目审核流程Controller
 * 
 * @author ruoyi
 * @date 2025-06-21
 */
@Controller
@RequestMapping("/system/test")
public class GysptTestController extends BaseController
{
    private String prefix = "system/test";

    @Autowired
    private IGysptTestService gysptTestService;
    @Autowired
    private ISysUserService userService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;

    @RequiresPermissions("system:test:view")
    @GetMapping()
    public String test()
    {
        return prefix + "/test";
    }
    
    /**
     * 查询请假列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GysptTest gysptTest)
    {
        SysUser user = getSysUser();
        String username = user.getLoginName();
        gysptTest.setUserId(username);
        startPage();
        List<GysptTest> list = gysptTestService.selectGysptTestList(gysptTest);
        return getDataTable(list);
    }
    
    /**
     * 部门领导审批
     * @return
     */
    @GetMapping("/level1")
    public String level1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GysptTest test = gysptTestService.selectGysptTestById(Long.parseLong(p.getBusinessKey().toString()));
            mmap.put("test", test);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            mmap.put("userlist", userService.selectUserList(new SysUser()));
        }
        return prefix + "/level1check";
    }
    
    @GetMapping("/level2")
    public String level2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GysptTest test = gysptTestService.selectGysptTestById(Long.parseLong(p.getBusinessKey().toString()));
            mmap.put("test", test);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            mmap.put("userlist", userService.selectUserList(new SysUser()));
        }
        return prefix + "/level2check";
    }
    
    @GetMapping("/level3")
    public String level3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GysptTest test = gysptTestService.selectGysptTestById(Long.parseLong(p.getBusinessKey().toString()));
            mmap.put("test", test);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
        }
        return prefix + "/level3check";
    }


    /**
     * 导出测试项目审核流程列表
     */
    @RequiresPermissions("system:test:export")
    @Log(title = "测试项目审核流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptTest gysptTest)
    {
        List<GysptTest> list = gysptTestService.selectGysptTestList(gysptTest);
        ExcelUtil<GysptTest> util = new ExcelUtil<GysptTest>(GysptTest.class);
        return util.exportExcel(list, "测试项目审核流程数据");
    }
    
    /**
     * 修改测试项目审核流程
     */
    @RequiresPermissions("system:test:add")
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增保存测试项目审核流程
     */
    @RequiresPermissions("system:test:add")
    @Log(title = "测试项目审核流程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptTest gysptTest){
    	gysptTest.setUpdateAt(new Date());
    	gysptTest.setCreateAt(new Date());
    	gysptTest.setCreateUser(getLoginName());
    	gysptTest.setUpdateUser(getLoginName());
    	gysptTest.setDeleteFlag(1);
    	gysptTest.setUserId(getLoginName());
        return toAjax(gysptTestService.insertGysptTest(gysptTest));
    }

    /**
     * 修改测试项目审核流程
     */
    @RequiresPermissions("system:test:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptTest gysptTest = gysptTestService.selectGysptTestById(id);
        mmap.put("gysptTest", gysptTest);
        return prefix + "/edit";
    }

    /**
     * 修改保存测试项目审核流程
     */
    @RequiresPermissions("system:test:edit")
    @Log(title = "测试项目审核流程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptTest gysptTest)
    {
    	gysptTest.setUpdateAt(new Date());
    	gysptTest.setUpdateUser(getLoginName());
        return toAjax(gysptTestService.updateGysptTest(gysptTest));
    }

}
